/*
 * Copyright (C) Weguide
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package humrum.weguide.views;

import humrum.weguide.R;
import humrum.weguide.util.Overlays;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class TabViewPoiMap extends MapActivity {

//	private static final int GPS_ENABLED_RESULT = 1;

	/** Map */
	private MapView map;
	private MapController controller;

	/** Poi coords */
	private Double latitude;
	private Double longitude;

	/** Geopoint */
	private GeoPoint _poiGeoPoint;

	/** enable satelite */
	private boolean satelite = false;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// Fullscreen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		initMap();
	}

	private void initMap() {
		setContentView(R.layout.mapview);

		map = (MapView) findViewById(R.id.mapview);
		controller = map.getController();
		map.setSatellite(false);
		// map.setBuiltInZoomControls(true);

		// set params on map
		initDrawOnMap();
	}

	private void initDrawOnMap() {
		// text
		final String label = this.getIntent().getExtras().getString("poiname");
		((TextView) findViewById(R.id.labelPoi)).setText(label);

		latitude = Double
				.valueOf(this.getIntent().getExtras().getString("lat")) * 1e6;

		longitude = Double.valueOf(this.getIntent().getExtras()
				.getString("lng")) * 1e6;

		Log.i("WeGuide", "" + latitude.intValue() + ", " + longitude.intValue());
		_poiGeoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
		animateTo(_poiGeoPoint);

		// try again
		final Overlays overlays = new Overlays(this.getResources().getDrawable(
				R.drawable.green_marker));
		final OverlayItem overlay = new OverlayItem(_poiGeoPoint, label, "");
		overlays.addOverlay(overlay);

		map.getOverlays().clear();
		map.getOverlays().add(overlays);
	}

	private void animateTo(GeoPoint geop) {
		controller.animateTo(geop);
		controller.setZoom(18);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		final MenuInflater inflate = getMenuInflater();
		inflate.inflate(R.menu.menu_map, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_map_mode:
			mapModSwitcher();
			return true;
		case R.id.menu_navigate:
			this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
					.parse("google.navigation:q=" + latitude + "," + longitude
							+ "-34.85254")));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void mapModSwitcher() {
		satelite = !satelite;
		map.setSatellite(satelite);
	}
}
